import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'

/* 用户 */
import Users from './components/user/Users.vue'
/* 权限 */
import Rights from './components/power/Rights.vue'
import Roles from './components/power/Roles.vue'

/* 商品管理 */
import Cate from './components/goods/Cate.vue'
import Params from './components/goods/Params.vue'
import List from './components/goods/List.vue'
import Add from './components/goods/Add.vue'
Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: "/login" },
    { path: '/login', name: 'login', component: Login },
    { path: '/home',
      name: 'home',
      component: Home, 
      redirect: "/welcome",
      children: [
        { path: '/welcome', name: 'Welcome', component: Welcome },
        { path: '/users', name: 'Users', component: Users},
        { path: '/rights', name: 'Rights', component: Rights},
        { path: '/roles', name: 'Roles', component: Roles},
        { path: '/categories', name: 'cate', component: Cate},
        { path: '/params', name: 'Params', component: Params},
        { path: '/goods', name: 'List', component: List},
        { path: '/goods/add', name: 'Add', component: Add},
        
      ]
    },
  ]
})




/*路由导航守卫*/
router.beforeEach((to, from, next) => {
  if (to.path == "/login") return next();
  // 获取token
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) return next("/login");
  next();
})

export default router


